﻿using Common;
using DBUtility;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseDal
{
    public class StoreDal
    {
        static readonly Lazy<StoreDal> dal = new Lazy<StoreDal>(() => new StoreDal());
        public static StoreDal Instance { get; } = dal.Value;
        private StoreDal() { }

        private const string tableName = "Store";

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from " + tableName);
            strSql.Append(" where IsDelete=0 " + strWhere);
            return SqlHelper.Exists(strSql.ToString());
        }

        /// <summary>
        /// 查询SQL语句
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public StringBuilder GetSelectSql(string strWhere = "")
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT ID,ParentId,StoreName,Status,Description,CreateTime,");
            sb.Append("Head,Phone,Type ");
            sb.Append("FROM " + tableName);
            sb.Append(" Where IsDelete=0 ");

            if (!string.IsNullOrEmpty(strWhere))
            {
                sb.Append(strWhere);
            }

            return sb;
        }

        /// <summary>
        /// 查询list
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Store> GetListModels(string strWhere, string filedOrder)
        {
            StringBuilder sb = GetSelectSql(strWhere);
            sb.Append(" order by " + filedOrder);
            DataSet ds = SqlHelper.Query(sb.ToString());
            List<Store> list = CommonOperation.ConvertDataTableToModelList<Store>(ds.Tables[0]);
            return list;
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Store GetModelById(int id)
        {
            StringBuilder sb = GetSelectSql("and ID=@id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@id",id)
            };
            DataSet ds = SqlHelper.Query(sb.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return CommonOperation.DataTableToModel<Store>(ds.Tables[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 新增仓库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddStore(Store model)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("INSERT INTO " + tableName + "(");
            sb.Append("ParentId,StoreName,Status,Description,");
            sb.Append("Head,Phone,CreateTime,Type)");
            sb.Append(" VALUES(");
            sb.Append("@parentId,@storeName,@status,@description,@head,@phone,");
            sb.Append("@createTime,@type)");
            SqlParameter[] parameters =
           {
                new SqlParameter("@parentId",model.ParentId),
                new SqlParameter("@storeName",model.StoreName),
                new SqlParameter("@status",model.Status),
                new SqlParameter("@description",string.IsNullOrEmpty(model.Description)?DBNull.Value:(object)model.Description),
                new SqlParameter("@head",model.Head),
                new SqlParameter("@phone",model.Phone),
                new SqlParameter("@createTime",model.CreateTime),
                new SqlParameter("@type",model.Type)
            };
            int res = SqlHelper.ExecuteSql(sb.ToString(), parameters);
            return res;
        }

        /// <summary>
        /// 修改仓库信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateStore(Store model)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Update " + tableName + " SET ");
            sb.Append("ParentId=@parentId,");
            sb.Append("StoreName=@storeName,");
            sb.Append("Description=@description,");
            sb.Append("Head=@head,");
            sb.Append("Phone=@phone,");
            sb.Append("Type=@type ");
            sb.Append("WHERE ID=@id");
            SqlParameter[] parameters = {
                new SqlParameter("@parentId",model.ParentId),
                new SqlParameter("@storeName",model.StoreName),
                new SqlParameter("@description",model.Description),
                new SqlParameter("@head",model.Head),
                new SqlParameter("@phone",model.Phone),
                new SqlParameter("@type",model.Type),
                new SqlParameter("@id",model.ID),
            };
            int result = SqlHelper.ExecuteSql(sb.ToString(), parameters);
            return result;
        }

        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool DeleteStore(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" update " + tableName + " set ");
            strSql.Append(" IsDelete=1 ");
            strSql.Append(" Where ID=" + ID);
            int result = SqlHelper.ExecuteSql(strSql.ToString());
            return result > 0;
        }

        /// <summary>
        /// 修改状态（启用、禁用）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool UpdateStatus(int id, int status)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Update " + tableName + " SET ");
            sb.Append("Status=@status ");
            sb.Append("WHERE ID=@id");
            SqlParameter[] parameters = {
                new SqlParameter("@status",status),
                new SqlParameter("@id",id),
            };
            int result = SqlHelper.ExecuteSql(sb.ToString(), parameters);
            return result > 0;
        }

    }
}
